<?php
class newOrEditCatalogAction extends sfAction
{
    public function execute($request)
    {
        $this->catalog_form = new CreateCatalogForm();
        unset($this->catalog_form[$this->catalog_form->getCSRFFieldName()]);
        $catalog_id = $request->getParameter('catalog_id', null);
        
        $this->catalog_form->bind($request->getParameter($this->catalog_form->getName()), $request->getFiles($this->catalog_form->getName()));
		$catalog_form = $request->getparameter("create_catalog_form");
        if ($catalog_form->isValid()) {
            if ($catalog_id) {
                $catalog = Doctrine_Core::getTable('catalog')->find($catalog_id);
                if (!$catalog)
                    return $this->renderText(json_encode(array('status' => 'update_fail')));
            } else {
                $catalog = new Catalog();
            }
            
            // update or create catalog
            $catalog->name = $catalog_form['name'];
            $catalog->is_active = $catalog_form['is_active'] == 'on' ? true : false;
            $catalog->description = $catalog_form['description'];
            $catalog->meta_title = $catalog_form['meta_title'];
            $catalog->meta_description = $catalog_form['meta_description'];
            $catalog->meta_keywords = $catalog_form['meta_keywords'];
            $catalog->updated_at = date(DATE_ISO8601);
            if (!$catalog_id)
                $catalog->created_at = date(DATE_ISO8601);
            // TODO: update parent category
			
			$catalog->save();
        }
        return sfView::SUCCESS;
        
    }
}
?>